// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CertificateServiceImp.java

package com.iphaeton.lms.business.certificate;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.certificate.Certificate;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.*;

// Referenced classes of package com.iphaeton.lms.business.certificate:
//            ICertificateService

public class CertificateServiceImp extends PersistentServiceSupport
    implements ICertificateService
{

    public CertificateServiceImp()
    {
    }

    public Certificate save(Certificate certificate)
        throws BusinessException
    {
        Certificate c = null;
        try
        {
            c = (Certificate)hbpservice.store(certificate);
            hbpservice.commit();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u4FDD\u5B58\u8BA4\u8BC1\u5230\u6570\u636E\u5E93\u65F6\u51FA\u9519!");
        }
        return c;
    }

    public void remove(String id)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.certificate.Certificate.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u4ECE\u6570\u636E\u5E93\u5220\u9664\u8BA4\u8BC1\u65F6\u51FA\u9519!");
        }
    }

    public Certificate findCertificateById(String id)
        throws BusinessException
    {
        Certificate c = null;
        try
        {
            c = (Certificate)hbpservice.get(id, com.iphaeton.lms.pojo.certificate.Certificate.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u8BE2\u8BA4\u8BC1\u51FA\u9519!");
        }
        return c;
    }

    public Pagination queryCertificate(String organize_id, int state, String row, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(c) from Certificate c where c.organizeId='" + organize_id + "' ";
        String hqlQueryStr = "from Certificate c where c.organizeId='" + organize_id + "' ";
        if(state != 0)
        {
            selectHql = selectHql + " and c.state='" + state + "'";
            hqlQueryStr = hqlQueryStr + " and c.state='" + state + "'";
        }
        if(row != null && !"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by c." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by c.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u4ECE\u6570\u636E\u5E93\u4E2D\u67E5\u8BE2\u8BA4\u8BC1\u5217\u8868\u65F6\u51FA\u9519!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List currentCertificate(String user_id, String id, String org_url, String row)
        throws BusinessException
    {
        List list = null;
        String sql = "";
        sql = sql + "select cert.id as id, cert.name as name, cert.type as type, state=0 ,cert.effective as effective,cert.certificate_date as date, p.start_time as start_time,p.end_time as end_time,p.id as paperId from t_certificate cert inner join t_paper p on cert.paper_id=p.id inner join t_organize o on o.id=cert.organize_id inner join t_group_certificate gc on gc.certificate_id=cert.id  and '" + org_url + "' like o.url" + "+'%'" + " and cert.state=1 and cert.id not in (select certificate_id from t_user_certificate where user_id='" + user_id + "') union select cert.id as id, cert.name as name, cert.type as type, state=0 ,cert.effective as effective,cert.certificate_date as date,p.start_time as start_time,p.end_time as end_time,p.id as paperId " + "from t_certificate cert inner join t_paper p on cert.paper_id=p.id inner join t_organize_certificate oc on oc.certificate_id=cert.id " + " and oc.organize_id='" + id + "' and cert.state=1 and cert.id not in (select certificate_id from t_user_certificate where user_id='" + user_id + "')";
        if(row == null || "".equals(row))
            sql = sql + "order by name";
        else
            sql = sql + "order by " + row;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5B66\u5458\u8BA4\u8BC1\u5217\u8868\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public void applyCertificate(String s, String s1)
        throws BusinessException
    {
    }

    public void getCertificate(String s, String s1)
        throws BusinessException
    {
    }

    public void setCertificateGroup(String cert_id, String group_id)
        throws BusinessException
    {
        String sql = "insert into t_group_certificate (certificate_id,group_id) values ('" + cert_id + "','" + group_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8BA4\u8BC1\u76F8\u5173\u5C97\u4F4D\u83B7\u804C\u52A1\u65F6\u51FA\u9519!");
        }
    }

    public void setCertificateOrganize(String cert_id, String organize_id)
        throws BusinessException
    {
        String sql = "insert into t_organize_certificate(certificate_id,organize_id) values ('" + cert_id + "','" + organize_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8BA4\u8BC1\u76F8\u5173\u7EC4\u7EC7\u7ED3\u6784\u65F6\u51FA\u9519!");
        }
    }

    public List findPageList(Pagination pageInfo)
    {
        List list = null;
        try
        {
            list = getSpecifiedItems(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    public List myCertificate(String user_id, String row)
        throws BusinessException
    {
        List list = null;
        String sql = "select cert.id as id, cert.name as name, cert.type as type,cert.certificate_date as date,cert.effective as effective,uc.certificate_time as time from t_certificate cert inner join t_user_certificate uc on cert.id = uc.certificate_id and uc.user_id  = '" + user_id + "' and uc.state=1";
        if(row == null || "".equals(row))
            sql = sql + " order by name";
        else
            sql = sql + " order by " + row;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7528\u6237\u5DF2\u901A\u8FC7\u7684\u8BA4\u8BC1\u5217\u8868\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public List getCertificateList(String url)
        throws BusinessException
    {
        String hql = "from Certificate c where c.organize.url like '" + url + "%' and c.state!=3 order by c.certificateDate";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u6307\u5B9A\u7EC4\u7EC7\u73B0\u6709\u8BA4\u8BC1\u5217\u8868\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public List certificateInfo(String id, int state)
        throws BusinessException
    {
        String sql = "select cert.id as id, cert.name as name, cert.type as type,cert.certificate_date as date,cert.effective as effective,uc.certificate_time as time,u.id as uid,u.user_id as user_id ,u.name as uname from t_certificate cert inner join t_user_certificate uc on uc.certificate_id=cert.id and uc.state=" + state + " inner join t_user u on u.id=uc.user_id";
        if(id != null)
            sql = sql + " and cert.id='" + id + "'";
        sql = sql + " order by cert.name";
        List list;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BA4\u8BC1\u83B7\u53D6\u60C5\u51B5\u51FA\u9519!");
        }
        return list;
    }

    public Document getTree(String id, String url, String state)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        List list = getCertificateList(url);
        for(int i = 0; i < list.size(); i++)
        {
            Certificate cs = (Certificate)list.get(i);
            Element csElement = element.addElement("tree");
            csElement.addAttribute("text", cs.getName());
            csElement.addAttribute("action", "../../certificateTxAction.do?method=certificateInfo&id=" + id + "&state=" + state);
            csElement.addAttribute("icon", "");
            csElement.addAttribute("openIcon", "");
            csElement.addAttribute("target", "operateFrame");
            csElement.addAttribute("expended", "false");
        }

        return doc;
    }

    public void setCertificateUser(String certificate_id, String user_id)
        throws BusinessException
    {
        Calendar cal = Calendar.getInstance();
        java.util.Date time = cal.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = df.format(time);
        String sql = "insert into t_user_certificate (certificate_id,user_id,certificate_time,state) values ('" + certificate_id + "','" + user_id + "','" + currentTime + "',3)";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u7528\u6237\u53C2\u52A0\u8BA4\u8BC1\u8003\u8BD5\u65F6\u51FA\u9519!");
        }
    }

    public void userCertificateUpdate(String certificate_id, String user_id, int state)
        throws BusinessException
    {
        Calendar cal = Calendar.getInstance();
        java.util.Date time = cal.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = df.format(time);
        String sql = "update t_user_certificate set state=" + state + ",certificate_time='" + currentTime + "' where certificate_id='" + certificate_id + "' and user_id='" + user_id + "'";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BA4\u8BC1\u8003\u8BD5\u5B8C\u6BD5\uFF0C\u4FEE\u6539\u8BA4\u7528\u6237\u8BC1\u72B6\u6001\u51FA\u9519!");
        }
    }

    public void certificateAgain(String cid, String uid, String pid)
        throws BusinessException
    {
        String sql = "delete from t_user_certificate where certificate_id='" + cid + "' and user_id='" + uid + "'";
        String sql1 = "delete from t_user_score where user_id='" + uid + "' and paper_id='" + pid + "'";
        try
        {
            jdbcpservice.execute(sql);
            jdbcpservice.execute(sql1);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u7528\u6237\u518D\u6B21\u8BA4\u8BC1\u65F6\u51FA\u9519!");
        }
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.certificate.CertificateServiceImp.class.getName());
    }
}
